#!/usr/bin/env bash
#
## 以下内容是对脚本的基本信息的描述
## Name: install_nginx.sh
## Update:2020-10-25
## auth：kaikai
## mail：jenkai136@sina.com

function isRoot () {
	if [ "$EUID" -ne 0 ]; then
		return 1
	fi
}
function checkOS () {
	if [[ -e /etc/debian_version ]]; then
		OS="debian"
		source /etc/os-release
		if [[ "$ID" == "debian" ]]; then
			if [[ ! $VERSION_ID =~ (8|9|10) ]]; then
				echo "⚠️ Your version of Debian is not supported."
				echo ""
				echo "However, if you're using Debian >= 9 or unstable/testing then you can continue."
				echo "Keep in mind they are not supported, though."
				echo ""
				until [[ $CONTINUE =~ (y|n) ]]; do
					read -rp "Continue? [y/n]: " -e CONTINUE
				done
				if [[ "$CONTINUE" = "n" ]]; then
					exit 1
				fi
			else
			  echo "Your version of Debian is $VERSION_ID"
			fi
		elif [[ "$ID" == "ubuntu" ]];then
			OS="ubuntu"
			if [[ ! $VERSION_ID =~ (16.04|18.04|19.04) ]]; then
				echo "⚠️ Your version of Ubuntu is not supported."
				echo ""
				echo "H如果你使用的是Ubuntu>17或beta，那么你可以继续."
				echo "Keep in mind they are not supported, though."
				echo ""
				until [[ $CONTINUE =~ (y|n) ]]; do
					read -rp "Continue? [y/n]: " -e CONTINUE
				done
				if [[ "$CONTINUE" = "n" ]]; then
					exit 1
				fi
			else
		    echo "Your version of Ubuntu is $VERSION_ID"
			fi

		fi
	elif [[ -e /etc/centos-release ]]; then
    OS=centos
    source /etc/os-release
		if ! grep -qs "^CentOS Linux release 7" /etc/centos-release; then
			echo "Your version of CentOS is not supported."
			echo "The script only support CentOS 7."
			echo ""
			unset CONTINUE
			until [[ $CONTINUE =~ (y|n) ]]; do
				read -rp "Continue anyway? [y/n]: " -e CONTINUE
			done
			if [[ "$CONTINUE" = "n" ]]; then
				echo "Ok, bye!"
				exit 1
			fi
		else
			  echo "Your version of Debian is $ID"
		fi

	else
		echo "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS or Arch Linux system"
		exit 1
	fi
}

function initialCheck () {
	if ! isRoot; then
		echo "Sorry, you need to run this as root"
		exit 1
	fi
	checkOS
}
function download_package() {
     git clone https://gitee.com/kaikai136412162/nginx_install.git
      cd nginx_install
      tar -xf nginx-1.14.2.tar.gz -C /usr/local/
      cp -Rf nginx-rtmp-module /usr/local/
}
function make_install() {

      cd /usr/local/nginx-1.14.2
      ./configure \
      --prefix=/usr/share/nginx \
      --conf-path=/etc/nginx/nginx.conf \
      --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log \
      --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid  --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
      --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module   --with-http_dav_module --with-http_addition_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module  --with-http_mp4_module --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module  --with-http_auth_request_module --with-http_v2_module --with-http_slice_module --with-threads --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --with-http_perl_module=dynamic --with-http_xslt_module=dynamic \
      --with-mail=dynamic --with-mail_ssl_module \
      --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module \
      --add-dynamic-module=/usr/local/nginx-rtmp-module/ \
      --modules-path=/usr/lib/nginx/modules

      make && make install
      mkdir -p /var/lib/nginx/body
}
function env_source() {
      echo "export PATH=/usr/share/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh
      source /etc/profile
}

function nginxCheck() {
    nginx -t
    nginx -s reload
}



function installnginx() {
    if [[ "$OS" =~ (debian|ubuntu) ]]; then
      echo "ubuntu install nginx"
      apt-get update -y
      apt-get -y install libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
      apt-get install -y gcc libperl-dev libgeoip-dev libgd-dev libpcre3 libpcre3-dev openssl libssl-dev
      apt-get install -y openssl-devel pcre-devel zlib-devel

      # 下載安裝包
      download_package
      # 編譯
      make_install
      # add 环境变量
      env_source
      echo "ubuntu install nginx ok"

    elif [[ "$OS" = 'centos' ]]; then
      yum update -y
      yum -y install libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
      yum install -y gcc libperl-dev
      yum install -y openssl-devel pcre-devel zlib-devel

      # 下載安裝包
      download_package
      # 編譯
      make_install
      # add 环境变量
      env_source

      echo "centos install nginx ok"
    rm -rf nginx_install
    fi
}

function checkStatus() {
    nginx -t
}
function restartNginx() {
    nginx -s reload
    echo "restart nginx ok"
}
function removeNginx() {
    if [[ "$OS" = 'centos' ]]; then
      yum remove nginx -y
      rm -rf `find / -name nginx`
      rm -rf /usr/local/nginx-*
      echo "remove nginx ok"
    elif [[ "$OS" = 'ubuntu' ]]; then
      apt-get remove nginx -y
      rm -rf `find / -name nginx`
      rm -rf /usr/local/nginx-*
      echo "remove nginx ok"
    fi
}

function manageMenu () {
	clear
	echo "Welcome to nginx!"
	echo ""
	echo "It looks like nginx is already installed."
	echo ""
	echo "What do you want to do?"
	echo "   1) 检查状态"
	echo "   2) 重启"
	echo "   3) Remove nginx"
	echo "   4) Exit"
	until [[ "$MENU_OPTION" =~ ^[1-4]$ ]]; do
		read -rp "Select an option [1-4]: " MENU_OPTION
	done

	case $MENU_OPTION in
		1)
		  checkStatus

		;;
		2)
			restartNginx
		;;
		3)
			removeNginx
		;;
		4)
			exit 0
		;;
	esac
}

# 检测是否root用户，检测系统类型
initialCheck

# 检查是否已安装nginx
if [[ -e /etc/nginx/nginx.conf ]]; then
  echo "$OS system installed nginx"
  manageMenu
else
	installnginx
	nginxCheck
fi

